package com.sky.generate;

import com.sky.frame.annotation.Title;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhijun.guo
 * @create 2019-02-06 22:40
 **/
public class GenerateTemplate {

    public final static int ROW_COL = 4;

    public final static String enter = System.getProperty("line.separator");

    public String replaceHTMLContent(String template, Class clazz) throws Exception {

        //TODO : 1.替换title 2.替换 --TONTENT-- 信息;

        Title clazzcnno = (Title)clazz.getAnnotation(Title.class);

        if(clazzcnno == null) throw new Exception(clazz + "没有配置相关注解!");

        template = template.replaceAll("--TITLE--",clazzcnno.title());

        template = template.replaceAll("--CONTROLLER--",clazz.getSimpleName().toLowerCase());

        template = template.replaceAll("--FILENAME--",clazz.getSimpleName() + "Search");

        Field[] fields = clazz.getDeclaredFields();
        //1.生成查询参数
        String queryContent = analysisGroupField(fields);

        template = template.replace("--QUERYGROUP--",queryContent);

        //生成更新表单数据
        String updateForm = analysisUpdateForm(fields);

        template = template.replace("--UPDATEGROUP--",updateForm);

        //2.生成表头
        String handlerCntent = analysisHandlerField(fields);
        template = template.replace("--DATAGRIDHANDLER--",handlerCntent);
        //3.生成表体
        String tableCntent = analysisDataGridField(fields);
        template = template.replace("--DATAGRIDCONTENT--",tableCntent);

        return template;
    }

    private String analysisUpdateForm(Field[] fields) {
        StringBuffer result = new StringBuffer();
        for(int i = 0; i < fields.length; i ++){
            Title title = fields[i].getAnnotation(Title.class);
            if(title == null || fields[i].getName().equals("id")) continue;
            result.append("<div class=\"search-row\">" + enter);
            result.append("<span class=\"col-xs-3\">"+title.title()+"</span>");
            result.append("<div class=\"col-xs-9\" data-control=\"TextInput\" path=\""+fields[i].getName()+"\"/>");
            result.append("</div>");
        }
        return result.toString();
    }

    public String analysisGroupField(Field[] fields){
        StringBuffer result = new StringBuffer();
        int colcount = 0;

        List<Field> titles = new ArrayList<>();

        for(int i = 0; i < fields.length; i ++){
            Title title = fields[i].getAnnotation(Title.class);
            if(title == null || title.show() == false) continue;
            titles.add(fields[i]);
        }

        for(int i = 0; i < titles.size(); i ++){
            Field field = titles.get(i);
            Title title = field.getAnnotation(Title.class);
            if(i == 0) result.append("<div class=\"search-row\">" + enter);
            result.append("<span class=\"col-xs-1\">"+title.title()+"</span>");
            result.append("<div class=\"col-xs-2\" data-control=\"TextInput\" path=\""+field.getName()+"\"/>" + enter);
            if(i > 0 && i+1 % (ROW_COL) == 0){
                result.append("</div>"+enter+"<div class=\"search-row\">" + enter);
            }else if(i == titles.size() - 1)
                result.append("</div>" + enter);
        }
        return result.toString();
    }

    public String analysisDataGridField(Field[] fields){
        StringBuffer result = new StringBuffer();
        for(Field field : fields){
            Title title = field.getAnnotation(Title.class);
            result.append("<div data-control=\"Label\" class=\"col-xs-1\" path=\""+field.getName()+"\"/>");
        }
        return result.toString();
    }

    public String analysisHandlerField(Field[] fields){
        StringBuffer result = new StringBuffer();
        for(Field field : fields){
            Title title = field.getAnnotation(Title.class);
            if(title == null) continue;
            result.append("<span class=\"col-xs-1\">"+title.title()+"</span>" + enter);
        }
        return result.toString();
    }

    public String replacejJSContent(String template, Class clazz) {

        template = template.replaceAll("JSCLASS",clazz.getSimpleName() + "Search");

        template = template.replaceAll("CONTROLLER",clazz.getSimpleName().toLowerCase());

        return template;
    }

    public String replacejClassContent(String template, Class clazz) {

        template = template.replaceAll("--CLASS_W_NAME--",clazz.getSimpleName().toLowerCase());

        template = template.replaceAll("--CLASS_NAME--",clazz.getSimpleName());

        return template;
    }
}
